//
//  NetRequest.m
//  棒棒哒
//
//  Created by rimi on 15/8/4.
//  Copyright (c) 2015年 rimi. All rights reserved.
//

#import "NetRequest.h"



@implementation NetRequest

+ (void)GET:(NSString *)url
 parameters:(NSDictionary *)parametersDic
    success:(void(^)(id responseObject))success
    failure:(void(^)(NSError *error))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer.timeoutInterval = 10;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/javascript", @"text/plain", nil];
//    manager.responseSerializer = [AFJSONRequestSerializer serializer];
    [manager GET:url parameters:parametersDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)request: (NSString*)httpUrl
    withHttpArg: (NSString*)HttpArg
        success:(void(^)(id responseObject))success
        failure:(void(^)(NSError *error))failure{
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, HttpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: @"dfe7dead7cec2ca2557830c7d6f028b5" forHTTPHeaderField: @"apikey"];
    [NSURLConnection sendAsynchronousRequest: request
                                       queue: [NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   failure(error);
                               } else {
                                   id obj = [self jsonWithData:data];
                                   success(obj);
                               }
                           }];
}

- (id)jsonWithData:(NSData *)data
{
    //1.创建error
    NSError *error = nil;
    //2.解析数据
    id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
    //3.创建错误字符串
    NSString *errorString = [NSString stringWithFormat:@"错误原因:%@",[error localizedDescription]];
    //4.断言
    NSAssert(!error, errorString);
    //5.返回obj
    return obj;
}

+(void)POST:(NSString *)url
 parameters:(NSDictionary *)parametersDic
    success:(void(^)(id responseObject))success
    failure:(void(^)(NSError *error))failure
{
    
    //初始化网络请求类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //配置请求超时时间
    manager.requestSerializer.timeoutInterval = 10;
    //设置MIME类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/javascript",@"text/plain", nil];
    //把字典参数转换为JSON Data 类型
    NSData *parametersData = [NSJSONSerialization dataWithJSONObject:parametersDic options:NSJSONWritingPrettyPrinted error:nil];
    //把Data类型转换成字符串
    NSString *parametersString = [[NSString alloc]initWithData:parametersData encoding:NSUTF8StringEncoding];
    //最终参数
    NSDictionary *parameterDic = @{@"paramJson":parametersString};
    
    [manager POST:url parameters:parameterDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

@end
